package com.zjq.controller;

import com.zjq.pojo.Books;
import com.zjq.pojo.Cart;
import com.zjq.pojo.Comments;
import com.zjq.pojo.Order;
import com.zjq.service.BookService;
import com.zjq.service.BookStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.print.Book;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/bookStore")
public class BookStoreController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @Autowired
    @Qualifier("BookStoreServiceImpl")
    private BookStoreService bookStoreService;

    @RequestMapping("/index")
    public String queryBookByType(Model model){
        List<Books> books = bookService.queryBookByType(1);
        List<Books> books1 = bookService.queryBookByType(3);
        List<Books> books2 = bookService.queryBookByKindL(1);
        List<Books> books3 = bookService.queryBookByKindL(7);
        List<Books> books4 = bookService.queryBookByKindL(3);
        List<Books> books5 = bookService.queryBookByKindL(2);
        model.addAttribute("kind3",books5);
        model.addAttribute("kind2",books4);
        model.addAttribute("week",books1);
        model.addAttribute("book",books);
        model.addAttribute("kind",books2);
        model.addAttribute("kind1",books3);
        return "bookstore/index";
    }

    /*详情页*/
    @RequestMapping("/todetail")
    public String toDetail(int bookid,Model model){
        Books books = bookService.queryBookById(bookid);
        System.out.println("=============================::::"+books.getBookId());
        List<Comments> comments1 = bookStoreService.queryCommentsByBookId(bookid);
        if (comments1.size()>0){
            System.out.println("+++++++++++++++++++++++++++++++++"+comments1.size());
            ArrayList al = new ArrayList();
            for (int i = 0;i < comments1.size();i++){
                ArrayList lists = new ArrayList();
                lists.add(comments1.get(i));
                List<Comments> comments = bookService.queryCommentsById(comments1.get(i).getCommentId());
                lists.add(comments);
                al.add(lists);
            }
            model.addAttribute("comments",al);
        }
        model.addAttribute("book",books);
        return "bookstore/book-details";
    }

    @RequestMapping("/toAddComment")
    public String toAddComment(int pId,int bookId,int userId,Model model){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        Comments comments = new Comments(format,format, pId, userId, bookId);
        System.out.println("-=--===-=-=-=++++++++++++++++++++"+comments.getpId());
        model.addAttribute("cs",comments);
        return "backstage/book/addcomment";
    }
    @RequestMapping("/addComment")
    public String AddComment(int pId,int bookId,int userId,String discuss) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sdf.format(date);
        Comments comments = new Comments(createTime,discuss, pId, userId, bookId);
        bookStoreService.addComment(comments);
        return "bookstore/cheat-1";
    }

    @RequestMapping("/deleteComment")
    public String deleteComment(int id){
        bookStoreService.deleteCommentById(id,0);
        bookStoreService.deleteCommentById(0,id);
        return "bookstore/cheat";
    }

    @RequestMapping("/addcart")
    public String addCart(int userId,int bookId,int counts){
        System.out.println("------------------------::::::::::::"+userId+bookId+counts);
        Cart cart = new Cart(userId,bookId,counts);
        List<Cart> carts = bookStoreService.queryCartByUserId(userId);
        Boolean T = true;
        for (Cart ct : carts){
            if (ct.getBookId() == bookId){
                bookStoreService.updateCartByid(ct.getCartId(),ct.getCounts()+1);
                T = false;
            }
        }
        if (T){
            bookStoreService.addCart(cart);
        }
        return "bookstore/cheat-1";
    }

    @RequestMapping("/queryCart")
    public String queryCart(int id,Model model){
        List<Cart> carts = bookStoreService.queryCartByUserId(id);
        model.addAttribute("carts",carts);
        return "bookstore/cart";
    }

    @RequestMapping("/queryCartByUserId")
    @ResponseBody
    public List<Cart> queryCartByUserId(int id){
        List<Cart> carts = bookStoreService.queryCartByUserId(id);
        return carts;
    }

    @RequestMapping("/updateCart")
    public void updateCart(int id,int counts){
        System.out.println("===================="+id+"============="+counts);
        bookStoreService.updateCartByid(id,counts);
    }

    @RequestMapping("/deletCartById")
    public String deletCartById(int id){
        bookStoreService.deletCartById(id);
        return "redirect:/bookStore/queryCart";
    }

    @RequestMapping("/toCheckOut")
    public String checkOut(int userId,Model model){
        List<Cart> carts = bookStoreService.queryCartByUserId(userId);
        model.addAttribute("carts",carts);
        return "bookstore/checkout";
    }

    @RequestMapping("/test")
    public String test(){
        return "bookstore/test";
    }
}
